#pragma once

#include "noncopyable.h"
#include "Timestamp.h"

#include <vector>
#include <unordered_map>

class Channel;
class EventLoop;

// 事件分发器（epoll/poll 的抽象）
class Poller : noncopyable
{
public:
    using ChannelList = std::vector<Channel *>;
    using ChannelMap = std::unordered_map<int, Channel *>;

    Poller(EventLoop *loop);
    virtual ~Poller() = default;

    // IO复用统一接口
    virtual Timestamp poll(int timeoutMs, ChannelList *activeChannels) = 0;
    virtual void updateChannel(Channel *channel) = 0;
    virtual void removeChannel(Channel *channel) = 0;

    // 判断channel是否在当前Poller中
    bool hasChannel(Channel *channel) const;

    // 获取具体的IO复用类型
    static Poller *newDefaultPoller(EventLoop *loop);

protected:
    ChannelMap channels_; // 储存的<fd,Channel>映射

private:
    EventLoop *ownerLoop_; // IO复用对应的事件循环
};